package com.centriole.service.workflow.util;

import com.github.pagehelper.PageInfo;

import java.util.HashMap;
import java.util.Map;

public class ResultJson {
    public static final String SUCCESS_MSG = "操作成功";
    public static final String ERROR_MSG = "操作失败";
    public static final String INNER_ERR_MSG = "系统异常";
    private int code;
    private String msg;
    private Integer count;
    private Object data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public ResultJson() {
        this.code = ResultCode.FAILED;
    }

    public ResultJson(int code, String msg, Integer count, Object data) {
        this.code = code;
        this.msg = msg;
        this.count = count;
        this.data = data;
    }

    public static ResultJson success() {
        return new ResultJson(ResultCode.SUCCESS, SUCCESS_MSG, null, true);
    }

    public static ResultJson success(String msg, Object data, Integer count) {
        return new ResultJson(ResultCode.SUCCESS, msg, count, data);
    }

    public static ResultJson success(String msg, Object data) {
        return new ResultJson(ResultCode.SUCCESS, msg, null, data);
    }

    public static ResultJson success(String msg) {
        return new ResultJson(ResultCode.SUCCESS, msg, null, null);
    }

    public static ResultJson success(Object data) {
        return new ResultJson(ResultCode.SUCCESS, SUCCESS_MSG, null, data);
    }

    public static ResultJson fail(String msg) {
        return new ResultJson(ResultCode.FAILED, msg, null, null);
    }

    public static ResultJson fail(String msg, Object data) {
        return new ResultJson(ResultCode.FAILED, msg, null, data);
    }


    public static ResultJson error(String msg) {
        return new ResultJson(ResultCode.INNER_SERVER_ERROR, msg, null, null);
    }

    public static Map<Integer, Object> buildMap() {
        Map<Integer, Object> resultMap = new HashMap<>();
        return resultMap;
    }

    public static ResultJson usePageInfo(PageInfo<?> pageInfo) {
        return new ResultJson(ResultCode.SUCCESS, "ok", (int) pageInfo.getTotal(), pageInfo.getList());
    }

}
